package turtle.system.invest;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;

public class Util {
	
	private static final byte LOG_TABLE[] = {
			-1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
			4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
			5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
			5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
			6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
			6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
			6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
			6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
			7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
			7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
			7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
			7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
			7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
			7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
			7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
			7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7 };	
	
	public static final int integerLog2(int x) {
		int s, t;
	    return ((s = x >> 16) > 0) 
	    		?((t = s >> 8) > 0) ? 24 + LOG_TABLE[t] : 16 + LOG_TABLE[s]
	    		:((t = x >> 8) > 0) ? 8 + LOG_TABLE[t] : LOG_TABLE[x];
	}
	
	public static int integerLog2QuickVersion(int v) {
		int r = (v > 0xFFFF ? 1 : 0) << 4; v >>= r;
		int shift = (v > 0xFF ? 1 : 0) << 3; v >>= shift; r |= shift;
		shift = (v > 0xF ? 1: 0) << 2; v >>= shift; r |= shift;
		shift = (v > 0x3 ? 1 : 0) << 1; v >>= shift; r |= shift;
		return (r |= (v >> 1));
	}
	
	public static void closeSiliently(Closeable closable) {
	    try {
	        if (closable != null) closable.close();
	    } catch (Throwable t) {
	    }
	}

    public static void skipLines(BufferedReader reader, int count) 
            throws IOException {
        for (int i = 0; i < count; ++i) {
            if (reader.readLine() == null) return;
        }
    }
}
